﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APIExplorer
{
    public partial class MainForm : Form, IMainView
    {

        MainController controller;

        public MainForm()
        {
            InitializeComponent();
            controller = new MainController(this, new MainViewModel { ThreeLegged = false, Method = "HEAD" });
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            controller.Initialize();
        }

        public event EventHandler ExecuteClicked;

        IMainViewModel model;
        public IMainViewModel DataSource
        {
            get
            {
                return model;
            }
            set
            {
                model = value;

                OnModelChanged();
            }
        }

        private void OnModelChanged()
        {
            if (model == null)
                return;
            textBoxCompanyAccountId.DataBindings.Add("Text", model, "CompanyAccountId");
            textBoxConsumerKey.DataBindings.Add("Text", model, "ConsumerKey");
            textBoxConsumerSecret.DataBindings.Add("Text", model, "ConsumerSecret");

            comboBoxMethod.DataBindings.Add("DataSource", model, "Methods");
            comboBoxMethod.DataBindings.Add("SelectedItem", model, "Method");


            textBoxRequestUrl.DataBindings.Add("Text", model, "RequestUrl");

            checkBox3Legged.DataBindings.Add("Checked", model, "ThreeLegged");
            textBoxToken.DataBindings.Add("Text", model, "Token");
            textBoxToken.DataBindings.Add("Enabled", model, "TokenEnabled");
            textBoxTokenSecret.DataBindings.Add("Text", model, "TokenSecret");
            textBoxTokenSecret.DataBindings.Add("Enabled", model, "TokenSecretEnabled");
            
            comboBoxReturnType.DataBindings.Add("DataSource", model, "AcceptedMimeTypes");
            comboBoxReturnType.DataBindings.Add("SelectedItem", model, "AcceptMimeType");

            textBoxContentType.DataBindings.Add("Text", model, "ContentType");
            textBoxRequest.DataBindings.Add("Text", model, "Request");

            textBoxResponse.DataBindings.Add("Text", model, "Response");

            textBoxDebugLog.DataBindings.Add("Text", model, "DebugLog");

            ValidateChildren();
        }

        private void checkBox3Legged_CheckStateChanged(object sender, EventArgs e)
        {
            this.ValidateChildren();
        }

        private void buttonExecute_Click(object sender, EventArgs e)
        {
            if (ExecuteClicked != null)
                ExecuteClicked(this, e);
        }

    }
}
